<?php

namespace app\zyhsapi\controller\v1;

use app\common\cachemodel\Recode;
use app\common\lib\Aes;
use app\common\lib\Alidayu;
use app\common\lib\exception\ApiException;
use app\common\lib\IAuth;
use app\common\lib\Util;
use app\zyhsapi\controller\ZyhsAuth;
use app\zyhsapi\model\ZyhsAppleidUser;
use app\zyhsapi\model\ZyhsAstrBindUser;
use app\zyhsapi\model\ZyhsMessageCenter;
use app\zyhsapi\model\ZyhsQq;
use app\zyhsapi\model\ZyhsRecode;
use app\zyhsapi\model\ZyhsUser;
use app\zyhsapi\model\ZyhsUserAccount;
use app\zyhsapi\model\ZyhsWechat;
use app\zyhsapi\model\ZyhsWeibo;
use app\zyhsapi\service\CacheZyhs;
use GuzzleHttp\Exception\GuzzleException;
use think\Env;
use think\Exception;
use think\Log;
use think\response\Json;
use XznsTools\cache\zyhs\CacheManageZyhs;

class Info extends ZyhsAuth
{
    /**
     * @var string
     */
    private $msg;

    /**
     * 个人中心绑定手机号
     * @return Json
     * @throws ApiException
     * @throws Exception
     */
    public function bindTel()
    {
        //检测用户是否已绑定
        if ($this->user['tel'])
        {
            return show(config('code.error'), '您已绑定手机号');
        }
        $tel = input('param.tel/s', '');
        $tel = (new Aes())->decryptWithOpenssl($tel, 'zyhsapp');
        $code = input('param.code/d', 0);
        $areaCode = input('param.area_code/d', 86);
        $mobileTel = $areaCode === 86 ? $tel : $areaCode . '-' . $tel;

        if (!$tel || !$code || strlen($code) != 6)
        {
            return show(config('code.error'), '验证码错误');
        }
        $appConfig = config('zyhsapp');
        // 短信登录时，检测验证码是否正确
        if (!empty($code))
        {
            if (!in_array($tel, $appConfig['mobile'], false)
                || ($areaCode === 86 && $code != substr($appConfig['code'] * (substr($tel, 3, 6)), 0, 6)))
            {
                //苹果和安卓应用商店审核账号，固定验证码为666666
                if (($tel == '13045794520' && $code == '666666')
                    || ($tel == '13261877781' && $code == '666666'))
                {

                } else
                {
                    //  validate 严格校验
                    $mobileCode = Alidayu::getInstance()->checkSmsIdentifyZyhs($tel, $areaCode, 'bind');

                    if ($code != $mobileCode)
                    {
                        return show(config('code.error'), '短信验证码错误');
                    }
                }
            }
        } else
        {
            return show(config('code.error'), '请输入验证码');
        }

        $user = $this->CheckUserTel($tel, $areaCode);
        $userModel = new ZyhsUser();
        if ($user && !empty($user))
        {
            // 当前账号没有订单 没有 档案 可以废弃合并到绑定手机号账号
            $wechatModel = new ZyhsWechat();
            $qqModel = new ZyhsQq();
            $weiboModel = new ZyhsWeibo();
            $appleidUserModel = new ZyhsAppleidUser();
            //判断用户是否绑定微信
            $wechatInfo = $wechatModel->where('uid', $this->user['id'])->field('id,unionid')->find();
            if ($wechatInfo)
            {   // 绑定了微信
                // 判断要绑定的手机号账号是否绑定了微信
                $wechatId2 = $wechatModel->where('uid', $user['id'])->value('id');
                if ($wechatId2)
                {
                    $loginToken = md5(uniqid(md5(microtime(true)), true));
                    CacheZyhs::set('bindTel:loginToken:' . $mobileTel, $loginToken, 3600);
                    return show(config('code.success'), '绑定失败', [
                        'type' => 2,
                        'access_token_info' => null,
                        'sync_recode_info' => [],
                        'tel_user_name' => $user['nickname'],
                        'login_token' => $loginToken,
                    ]);
                }
            }
            //判断用户是否绑定了qq
            $qqInfo = $qqModel->where('uid', $this->user['id'])->field('id,unionid')->find();
            if ($qqInfo)
            {   // 绑定了QQ
                // 判断要绑定的手机号账号是否绑定了QQ
                $qqId2 = $qqModel->where('uid', $user['id'])->value('id');
                if ($qqId2)
                {
                    $loginToken = md5(uniqid(md5(microtime(true)), true));
                    CacheZyhs::set('bindTel:loginToken:' . $mobileTel, $loginToken, 3600);
                    return show(config('code.success'), '绑定失败', [
                        'type' => 2,
                        'access_token_info' => null,
                        'sync_recode_info' => [],
                        'tel_user_name' => $user['nickname'],
                        'login_token' => $loginToken,
                    ]);
                }
            }
            //判断用户是否绑定了微博
            $wbInfo = $weiboModel->where('uid', $this->user['id'])->field('id,wbuid')->find();
            if ($wbInfo)
            {   // 绑定了微博
                // 判断要绑定的手机号账号是否绑定了微博
                $wbId2 = $weiboModel->where('uid', $user['id'])->value('id');
                if ($wbId2)
                {
                    $loginToken = md5(uniqid(md5(microtime(true)), true));
                    CacheZyhs::set('bindTel:loginToken:' . $mobileTel, $loginToken, 3600);
                    return show(config('code.success'), '绑定失败', [
                        'type' => 2,
                        'access_token_info' => null,
                        'sync_recode_info' => [],
                        'tel_user_name' => $user['nickname'],
                        'login_token' => $loginToken,
                    ]);
                }
            }
            //判断用户是否绑定了apple
            $appleInfo = $appleidUserModel->where('uid', $this->user['id'])->field('id,apple_id')->find();
            if ($appleInfo)
            {   // 判断要绑定的手机号账号是否绑定了微博
                $appleId2 = $appleidUserModel->where('uid', $user['id'])->value('id');
                if ($appleId2)
                {
                    $loginToken = md5(uniqid(md5(microtime(true)), true));
                    CacheZyhs::set('bindTel:loginToken:' . $mobileTel, $loginToken, 3600);
                    return show(config('code.success'), '绑定失败', [
                        'type' => 2,
                        'access_token_info' => null,
                        'sync_recode_info' => [],
                        'tel_user_name' => $user['nickname'],
                        'login_token' => $loginToken,
                    ]);
                }
            }
            // 开始进行绑定 合并
            $id = (int)($user['id']);
            if ($wechatInfo)
            { // 微信合并
                $wechatModel->where('id', $wechatInfo['id'])->update(['uid' => $id]);
                CacheManageZyhs::getInstance()->setCacheConfig('wechat.wechat', $wechatInfo['unionid'] . '-1')->rmCache();
            }
            if ($qqInfo)
            { // QQ合并
                $qqModel->where('id', $qqInfo['id'])->update(['uid' => $id]);
                CacheManageZyhs::getInstance()->setCacheConfig('user.qq', $qqInfo['unionid'] . '-1')->rmCache();
            }
            if ($wbInfo)
            { // 微博合并
                $weiboModel->where('id', $wbInfo['id'])->update(['uid' => $id]);
                CacheManageZyhs::getInstance()->setCacheConfig('user.weibo', $wbInfo['wbuid'] . '-1')->rmCache();
            }
            if ($appleInfo)
            { // apple合并
                $appleidUserModel->where('id', $appleInfo['id'])->update(['uid' => $id]);
                CacheManageZyhs::getInstance()->setCacheConfig('user.apple', $appleInfo['apple_id'])->rmCache();
            }
            // 使用绑定手机号账号登录
            $token = IAuth::setAppLoginToken($tel); // 生成token
            $data = [
                'token' => $token,
                'token_expire' => strtotime("+" . $appConfig['login_time_out_day'] . " days"),
            ];
            // 更新用户信息
            $userModel->updateUserinfo(['id' => $id], $data);
            // 更新旧数据信息
            $userModel->updateUserinfo(['id' => $this->user['id']], ['token' => '', 'token_expire' => '']);

            // 获取自己档案ID
            $recordInfo = (new ZyhsRecode())->getRecodeLists($id, 1);
            $result = [
                'id' => $id,
                'self_rid' => $recordInfo ? (int)($recordInfo['id'] ?? 0) : 0,
                'user_access_token' => (new Aes())->encryptWithOpenssl($token . "||" . $user['id'], 'zyhsapp'),
                'has_bind_tel' => 1,     // 是否绑定了手机号 1绑定了 0未绑定
            ];

            return show(config('code.success'), '绑定成功', [
                'type' => 1,
                'access_token_info' => $result,
                'sync_recode_info' => [],
                'tel_user_name' => '',
                'login_token' => '',
            ]);
        }

        // 若未注册
        try
        {
            // 开始绑定手机号
            $bindInfo = ['tel' => $mobileTel, 'temporary_tel' => '', 'tel_bind_time' => 0];
            $userModel->updateUserinfo(['id' => $this->user['id']], $bindInfo);
            // 绑定成功 查看手机号是否
            $syncRecodeInfo = (new ZyhsRecode())->getAccountRecodeNumByTelZyhs($mobileTel, $this->user['id']);
        }
        catch (\Exception $e)
        {
            Log::write($this->user['id'] . '绑定手机号失败：' . $e->getMessage() . '【' . $e->getFile() . $e->getLine() . '】', 'error', true);
            return show(0, '绑定失败');
        }

        return show(config('code.success'), '绑定成功', [
            'type' => 1,
            'access_token_info' => null,
            'sync_recode_info' => $syncRecodeInfo,
            'tel_user_name' => '',
            'login_token' => '',
        ]);
    }

    /**
     * 更换手机
     * @return Json
     * @throws ApiException
     */
    public function changeTel()
    {
        $aesLib = new Aes();
        $old_tel = input('param.old_tel/s', '');
        $old_tel = $aesLib->decryptWithOpenssl($old_tel, 'zyhsapp');
        $tel = input('param.tel/s', '');
        $tel = $aesLib->decryptWithOpenssl($tel, 'zyhsapp');
        $code = input('param.code/d', 0);
        $oldAreaCode = input('param.old_area_code/d', 86);
        $areaCode = input('param.area_code/d', 86);

        $userOldTel = $this->user['tel'];
        $paramOldTel = $oldAreaCode === 86 ? $old_tel : $oldAreaCode . '-' . $old_tel;
        if ($userOldTel !== $paramOldTel)
        {
            return show(config('code.error'), '原手机号错误');
        }

        $mobileTel = $areaCode === 86 ? $tel : $areaCode . '-' . $tel;
        $user = $this->CheckUserTel($tel, $areaCode, '新手机号错误');
        if (!$old_tel || !$tel || !$code || strlen($code) != 6)
        {
            return show(config('code.error'), '验证码错误');
        }
        if ($user)
        {
            return show(config('code.error'), '该手机号已被其他账号绑定');
        }
        // 若未注册
        try
        {
            // 判断code是否正确
            $appConfig = config('zyhsapp');
            // 短信登录时，检测验证码是否正确
            if (!empty($code))
            {
                if (!in_array($tel, $appConfig['mobile'], false)
                    || ($areaCode === 86 && $code != substr($appConfig['code'] * (substr($tel, 3, 6)), 0, 6)))
                {
                    //苹果和安卓应用商店审核账号，固定验证码为666666
                    if (($tel == '13045794520' && $code == '666666')
                        || ($tel == '13261877781' && $code == '666666'))
                    {

                    } else
                    {
                        //  validate 严格校验
                        $mobileCode = Alidayu::getInstance()->checkSmsIdentifyZyhs($tel, $areaCode, 'bind');
                        if ($code != $mobileCode)
                        {
                            return show(config('code.error'), '短信验证码错误');
                        }
                    }
                }
            } else
            {
                return show(config('code.error'), '请输入验证码');
            }

            (new ZyhsUser())->updateUserinfo(['id' => $this->user['id']], ['tel' => $mobileTel]);
        }
        catch (Exception $e)
        {
            Log::write('更换手机号失败：' . $e->getMessage() . '失败行数：' . $e->getLine(), 'error', true);
            return show(config('code.error'), '验证码错误');
        }

        return show(config('code.success'), '操作成功');
    }

    /**
     * 三方绑定
     * @return Json
     * @throws ApiException
     * @throws GuzzleException
     * @throws Exception
     */
    public function bindThird()
    {
        $param = input('param.param');
        $param = $param ? json_decode($param, true) : [];
        $type = input('param.type/d');
        $this->msg = '';
        switch ($type)
        {
            case 1:
                $header = request()->header('app-type');
                if ($header == 'ios')
                {
                    $wechat_user = $param;
                } else
                {
                    $wechat_user = (new ZyhsWechat())->WechatInfo($param);
                }
                $res = $this->wechat($wechat_user);
                break;
            case  2:
                $res = $this->qq($param);
                break;
            case  3:
                $res = $this->weibo($param);
                break;
            case  5:
                $res = $this->appleId($param);
                break;
            default:
                return show(config('code.error'), '未知的账号');
        }
        if ($this->msg)
        {
            return show(config('code.error'), $this->msg);
        }
        return show(200, '绑定成功', ['nickname' => $res]);
    }

    /**
     * 微信登录
     * @param $wechat_user
     * @return string
     * @throws Exception
     */
    protected function wechat($wechat_user)
    {
        //检测用户是否已绑定
        $WechatModel = new ZyhsWechat();
        $user_wechat = $WechatModel->getWechatInfo($this->user['id'], 2);
        if ($user_wechat)
        {
            $this->msg = '您已绑定微信';
            return false;
        }
        //验证是否注册过
        $User = $WechatModel->getWechatInfo($wechat_user['unionid']);
        if ($User)
        {
            $this->msg = '当前微信已被注册';
            return false;
        }
        try
        {
            $wechat_insert = [
                'uid' => $this->user['id'],
                'nickname' => $wechat_user['nickname'] ?? '匿名用户',
                'sex' => $wechat_user['sex'] ?? 2,
                'city' => $wechat_user['city'] ?? '',
                'province' => $wechat_user['province'] ?? '',
                'country' => $wechat_user['country'] ?? '',
                'headimgurl' => $wechat_user['headimgurl'] ?? '',
                'openid' => $wechat_user['openid'],
                'unionid' => $wechat_user['unionid'],
                'create_time' => time(),
                'update_time' => time(),
            ];
            //qq表生成数据
            $WechatModel->SaveData($wechat_insert);

            return $wechat_user['nickname'];
        }
        catch (Exception $e)
        {
            $this->msg = '微信绑定失败';
        }
        return false;
    }

    /**
     * QQ登录
     * @param $param
     * @return string
     * @throws ApiException
     * @throws GuzzleException
     * @throws Exception
     */
    protected function qq($param)
    {
        //检测用户是否已绑定
        $QqModel = new ZyhsQq();
        $user_qq = $QqModel->getUserQqInfoFromCache($this->user['id'], 2);
        if ($user_qq)
        {
            return $this->msg = '您已绑定qq';
        }
        // 获取用户qq信息
        $qq_user = $QqModel->QqInfo($param);
        // 验证是否注册过
        $User = $QqModel->getUserQqInfoFromCache($qq_user['unionid']);
        if ($User)
        {
            $this->msg = '当前qq号已被注册';
            return false;
        }
        try
        {
            $qq_insert = [
                'uid' => $this->user['id'],
                'openid' => $param['openid'],
                'unionid' => $qq_user['unionid'],
                'create_time' => time(),
                'nickname' => $qq_user['nickname'],
                'sex' => $qq_user['sex'],
                'avatar' => $qq_user['figureurl_qq_1'],
            ];
            //qq表生成数据
            $QqModel->SaveData($qq_insert);
            return $qq_user['nickname'];
        }
        catch (Exception $e)
        {
            $this->msg = 'qq绑定失败';
        }
        return false;
    }

    /**
     * 微博登录
     * @param $param
     * @return string
     * @throws ApiException
     * @throws GuzzleException
     * @throws Exception
     */
    protected function weibo($param)
    {
        $WbModel = new ZyhsWeibo();
        //检测用户是否已绑定微博
        $user_wb = $WbModel->getUserWbInfoFromCache($this->user['id'], 2);
        if ($user_wb)
        {
            $this->msg = '您已绑定微博';
            return false;
        }
        // 获取weibo信息
        $wb_user = $WbModel->WeiboInfo($param);
        // 验证当前微博是否注册过
        $User = $WbModel->getUserWbInfoFromCache($param['uid']);
        if ($User)
        {
            $this->msg = '当前微博已被注册';
            return false;
        }
        try
        {
            $wb_insert = [
                'uid' => $this->user['id'],
                'wbuid' => $param['uid'],
                'create_time' => time(),
                'nickname' => $wb_user['name'],
                'sex' => $wb_user['sex'],
                'avatar' => $wb_user['profile_image_url'],
            ];
            //wb表生成数据
            $WbModel->SaveData($wb_insert);
            return $wb_user['name'];
        }
        catch (Exception $e)
        {
            $this->msg = '微博绑定失败';
        }
        return false;
    }

    /**
     * 苹果绑定
     * @param $param
     * @return false|string
     * @throws ApiException
     * @throws \think\Exception
     */
    protected function appleId($param)
    {
        $dbModel = new ZyhsAppleidUser();
        if ($dbModel->getAppleId($this->user['id']))
        {
            $this->msg = '您已绑定Apple ID';
            return false;
        }
        if (!isset($param['apple_id']))
        {
            $this->msg = '请求异常；绑定失败';
            return false;
        }
        // 验证是绑定
        $user = $dbModel->getUserInfo($param['apple_id']);
        if ($user)
        {
            if ($user['uid'] != $this->user['id'])
            {
                $this->msg = '该Apple ID已绑定其他账号';
                return false;
            } else
            {
                $dbModel->rmHasBindAppleid($this->user['id']);
                return '已绑定';
            }
        }
        try
        {
            $chennelId = Util::getChannelId();
            $userData = [
                'apple_id' => $param['apple_id'] ?? '',
                'nickname' => $param['name'] ?? '',
                'email' => $param['email'] ?? '',
                'uid' => (int)$this->user['id'],
                'channel_id' => $chennelId,
                'create_time' => time(),
            ];
            // 写入apple信息
            $dbModel->isUpdate(false)->allowField(true)->save($userData);
            $dbModel->rmHasBindAppleid($this->user['id']);
            return '已绑定';
        }
        catch (\Exception $e)
        {
            Log::write('绑定苹果异常:' . $e->getMessage(), 'error', true);
            $this->msg = '服务异常；绑定失败';
            return false;
        }
    }

    /**
     * 三方解绑
     * @return Json
     * @throws Exception
     */
    public function ubindThird()
    {
        $type = input('param.type/d');
        //若用户其他无绑定则不允许解绑
        $zyhsWechatModel = new ZyhsWechat();
        $zyhsQqModel = new ZyhsQq();
        $zyhsWeiboModel = new ZyhsWeibo();
        $zyhsAppleidUserModel = new ZyhsAppleidUser();
        switch ($type)
        {
            case 1:
                if (!$this->user['tel'])
                {
                    //判断用户是否绑定了qq
                    $qq_info = $zyhsQqModel->getUserQqInfoFromCache($this->user['id'], 2);
                    if (!$qq_info)
                    {
                        //判断用户是否绑定了微博
                        $wb_info = $zyhsWeiboModel->getUserWbInfoFromCache($this->user['id'], 2);
                        if (!$wb_info)
                        {
                            if (!$zyhsAppleidUserModel->getAppleId($this->user['id']))
                            {
                                return show(config('code.error'), '解绑失败');
                            }
                        }
                    }
                }
                $zyhsWechatModel->DelData($this->user['id']);
                break;
            case  2:
                if (!$this->user['tel'])
                {
                    //判断用户是否绑定微信
                    $wechat_info = $zyhsWechatModel->getWechatInfo($this->user['id'], 2);
                    if (!$wechat_info)
                    {
                        //判断用户是否绑定了微博
                        $wb_info = $zyhsWeiboModel->getUserWbInfoFromCache($this->user['id'], 2);
                        if (!$wb_info)
                        {
                            if (!$zyhsAppleidUserModel->getAppleId($this->user['id']))
                            {
                                return show(config('code.error'), '解绑失败');
                            }
                        }
                    }
                }
                $zyhsQqModel->DelData($this->user['id']);
                break;
            case  3:
                if (!$this->user['tel'])
                {
                    //判断用户是否绑定微信
                    $wechat_info = $zyhsWechatModel->getWechatInfo($this->user['id'], 2);
                    if (!$wechat_info)
                    {
                        //判断用户是否绑定了qq
                        $qq_info = $zyhsQqModel->getUserQqInfoFromCache($this->user['id'], 2);
                        if (!$qq_info)
                        {
                            if (!$zyhsAppleidUserModel->getAppleId($this->user['id']))
                            {
                                return show(config('code.error'), '解绑失败');
                            }
                        }
                    }
                }
                $zyhsWeiboModel->DelData($this->user['id']);
                break;
            case  5:
                if (!$this->user['tel'])
                {
                    //判断用户是否绑定微信
                    $wechat_info = $zyhsWechatModel->getWechatInfo($this->user['id'], 2);
                    if (!$wechat_info)
                    {
                        //判断用户是否绑定了qq
                        $qq_info = $zyhsQqModel->getUserQqInfoFromCache($this->user['id'], 2);
                        if (!$qq_info)
                        {
                            //判断用户是否绑定了微博
                            $wb_info = $zyhsWeiboModel->getUserWbInfoFromCache($this->user['id'], 2);
                            if (!$wb_info)
                            {
                                return show(config('code.error'), '解绑失败');
                            }
                        }
                    }
                }
                $zyhsAppleidUserModel->DelData((int)$this->user['id']);
                break;
            default:
                return show(config('code.error'), '未知的账号');
        }

        return show(config('code.success'), '解绑成功');
    }

    public function bindStatusInfo()
    {
        $resData = [
            'tel' => $this->user['tel'] ? preg_replace('/^([\w-]{3})([\w]*)([0-9]{4}$)/i', '$1****$3', $this->user['tel']) : '',
        ];

        //判断用户是否绑定微信
        $wechat_info = (new ZyhsWechat())->getWechatInfo($this->user['id'], 2);
        if ($wechat_info)
        {
            $resData['wechat'] = $wechat_info['nickname'];
        } else
        {
            $resData['wechat'] = '';
        }
        //判断用户是否绑定了qq
        $qq_info = (new ZyhsQq())->getUserQqInfoFromCache($this->user['id'], 2);
        if ($qq_info)
        {
            $resData['qq'] = $qq_info['nickname'];
        } else
        {
            $resData['qq'] = '';
        }
        //判断用户是否绑定了微博
        $wb_info = (new ZyhsWeibo())->getUserWbInfoFromCache($this->user['id'], 2);
        if ($wb_info)
        {
            $resData['weibo'] = $wb_info['nickname'];
        } else
        {
            $resData['weibo'] = '';
        }

        if ($this->headers['app-type'] === 'ios')
        {
            $resData['is_bind_apple'] = (new ZyhsAppleidUser())->getAppleId($this->user['id']) ? 1 : 0; // 是否绑定苹果ID
        }

        return show(config('code.success'), '请求成功', $resData);
    }

    /**
     * 个人中心
     */
    public function userCenter()
    {
//        $newMsgIds = (new ZyhsMessageCenter())->getAllUserMessage(['uid' => $this->user['id'], 'type' => 1]);
        // 订阅状态
        $oss = Env::get('host.photo');
        if ($this->user)
        {
            $nickname = $this->user['nickname'];
            $avatar = $this->user['avatar'];
            $uen = $this->user['uen'];
            $zyhsUserAccountModel = new ZyhsUserAccount();
            $user_coin = $zyhsUserAccountModel->getUserCoinFromCache($this->user['id']);  // 账户信息
            $balance = (int)($user_coin['balance'] ?? 0);// 准币余额
        } else
        {
            $nickname = '';
            $avatar = '';
            $uen = '';
            $balance = 0;
        }

        $list1 = [
            [
                'img' => $oss . 'zyhsapp/personal/module_wallet.png',
                'des' => "钱包：{$balance}币",
                'target' => 'local',
                'link' => 'wallet?need_auth=2',
            ], [
                'img' => $oss . 'zyhsapp/personal/module_chat.png',
                'des' => '我的连线',
                'target' => 'local',
                'link' => 'chat_list?need_auth=2',
            ],
        ];

        if ((!Util::IsIos() && Util::CheckAppVersion('1.0.0') === '大') || (Util::IsIos() && Util::CheckAppVersion('1.1.1') !== '小'))
        {
            $list2 = [
                [
                    'img' => $oss . 'zyhsapp/personal/question.png',
                    'des' => '我的提问',
                    'target' => 'local',
                    'link' => 'my_question?need_auth=2',
                ],
            ];
        } else
        {
            $list2 = [];
        }

        $list3 = [
            [
                'img' => $oss . 'zyhsapp/personal/module_follow.png',
                'des' => '我的关注',
                'target' => 'local',
                'link' => 'follow_list?need_auth=2',
            ],
            [
                'img' => $oss . 'zyhsapp/personal/module_coupon.png',
                'des' => '卡券中心',
                'target' => 'local',
                'link' => 'coupon_list?need_auth=2',
            ], [
                'img' => $oss . 'zyhsapp/personal/module_recode.png',
                'des' => '档案列表',
                'target' => 'local',
                'link' => 'recode_list?need_auth=2',
            ], [
                'img' => $oss . 'zyhsapp/personal/module_message_center.png',
                'des' => '系统消息',
                'target' => 'local',
                'link' => 'message_center?need_auth=2',
            ], [
                'img' => $oss . 'zyhsapp/personal/module_setting.png',
                'des' => '设置',
                'target' => 'local',
                'link' => 'setting?need_auth=0',
            ],
        ];
        $list = array_merge($list1, $list2, $list3);
        $astrBindInfo = (new ZyhsAstrBindUser())->getAstrBindUserData();
        $resData = [
            'list' => $list,
            'avatar' => $avatar,
            'nickname' => $nickname,
//            'new_msg' => count($newMsgIds),
            'uen' => $uen,
            'astr_id' => $astrBindInfo[$this->user['id']] ?? 0
        ];

        return show(config('code.success'), '请求成功', $resData);
    }

    public function userInfo()
    {
        $resData = [
            'avatar' => $this->user['avatar'],
            'nickname' => $this->user['nickname'],
            'tel' => $this->user['tel'] ? preg_replace('/^([\w-]{3})([\w]*)([0-9]{4}$)/i', '$1****$3', $this->user['tel']) : '',
            'is_bind_apple' => 0,
        ];

        //判断用户是否绑定微信
        $wechat_info = (new ZyhsWechat())->getWechatInfo($this->user['id'], 2);
        $resData['wechat'] = $wechat_info ? $wechat_info['nickname'] : '';

        if ($this->headers['app-type'] === 'ios')
        {
            $resData['is_bind_apple'] = (new ZyhsAppleidUser())->getAppleId($this->user['id']) ? 1 : 0; // 是否绑定苹果ID
        }
        $astrBindInfo = (new ZyhsAstrBindUser())->getAstrBindUserData();
        $resData['astr_id'] = $astrBindInfo[$this->user['id']] ?? 0;
        return show(config('code.success'), '请求成功', $resData);
    }

    public function updateInfo()
    {
        $type = input('type/d', 0);
        $value = input('value/s', '');

        if (!in_array($type, [1, 2], true))
        {
            return show(config('code.error'), '请求失败，编辑类型异常');
        }

        if (empty($value))
        {
            return show(config('code.error'), '请求异常，请稍后再试～');
        }

        if ($type === 2 && mb_strlen($value) > 10)
        {
            return show(config('code.error'), '编辑失败，昵称最多10个字～');
        }

        if ($type === 1)
        {
            $update = ['avatar' => $value];
        } else
        {
            $update = ['nickname' => $value];
        }

        // 更新 用户信息
        (new ZyhsUser())->updateUserinfo(['id' => $this->user['id']], $update);

        if ($this->user['rid'])
        {
            if ($type === 2)
            {
                $update = ['name' => $value];
            }

            (new ZyhsRecode())->UpdateData($this->user['id'], $this->user['rid'], $update);
        }

        return show(config('code.success'), '请求成功');
    }
}
